package com.shuai.common.constant;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;


@Slf4j
public abstract class EmailConstant {
    public static final String USER_EMAIL_KEY = "user_email_key";   // 用户邮箱验证码key值

    public static final Long TIME_OUT = 10L;        // 邮箱验证码过期时间

    public static final TimeUnit TIME_UNIT = TimeUnit.MINUTES;  // 验证码过期时间单位

    public static final String PROMPT; // 提词
    public static final String WARNING;    // 警告
    public static final String SUBJECT;    // 主题

    public static final int DIGIT = 6;  // 验证码位数


    static {
        ClassPathResource classPathResourcePrompt = new ClassPathResource("email/prompt.txt");
        ClassPathResource classPathResourceSubject = new ClassPathResource("email/subject.txt");
        ClassPathResource classPathResourceWarning = new ClassPathResource("email/warning.txt");

        try (InputStream inputStreamPrompt = classPathResourcePrompt.getInputStream();
             InputStream inputStreamSubject = classPathResourceSubject.getInputStream();
             InputStream inputStreamWarning = classPathResourceWarning.getInputStream();
             InputStreamReader promptReader = new InputStreamReader(inputStreamPrompt);
             InputStreamReader subjectReader = new InputStreamReader(inputStreamSubject);
             InputStreamReader warningReader = new InputStreamReader(inputStreamWarning);) {

            StringBuilder promptBuilder = new StringBuilder();
            StringBuilder warningBuilder = new StringBuilder();
            StringBuilder subjectBuilder = new StringBuilder();
            char[] buffer = new char[1024];
            int length = 0;
            while ((length = promptReader.read(buffer)) != -1) {
                promptBuilder.append(buffer, 0, length - 1);
            }
            while ((length = subjectReader.read(buffer)) != -1) {
                subjectBuilder.append(buffer, 0, length - 1);
            }
            while ((length = warningReader.read(buffer)) != -1) {
                warningBuilder.append(buffer, 0, length - 1);
            }
            // 配置输出邮箱发送的内容实例
            log.info("\n\n---------邮箱发送内容（示例）---------\n");

            PROMPT = promptBuilder.toString();
            SUBJECT = subjectBuilder.toString();
            WARNING = warningBuilder.toString();
            log.info(SUBJECT);
            log.info(PROMPT);
            log.info("546837（示例验证码）");
            log.info(WARNING + "\n\n");


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
